version: '3.3'

services:
  elasticsearch:
    image: elasticsearch:7.14.1
    restart: always
    ports:
      - "127.0.0.1:9200:9200"
    volumes:
      - ./esdata:/usr/share/elasticsearch/data
      - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
    environment:
      - discovery.type=single-node
      - ES_JAVA_OPTS=-Xms750m -Xmx750m
    deploy:
      resources:
        limits:
          memory: "3G"
        reservations:
          memory: "2G"
    healthcheck:
      test: "curl -s localhost:9200 > /dev/null || exit 1"
      interval: 10s
      timeout: 5s
      retries: 10
    networks:
      - knowledge-base-elasticsearch-network

  web:
    image: nginx
    ports:
      - "443:443"
      - "80:80"
    restart: always
    deploy:
      resources:
        limits:
          memory: "200Mb"
        reservations:
          memory: "100Mb"
    volumes:
      # Медиа - картинки, книги
      - ./media:/var/www/media:ro
      # Статика - CSS, JS
      - ./static:/var/www/static:ro

      # Настройки nginx
      - ./settings/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      # Приватный ключ
      - /etc/ssl/taged/private:/etc/ssl/private/:ro
      # Сертификат и ключи Диффи-Хеллмана
      - /etc/ssl/taged/certs:/etc/ssl/certs/:ro
    depends_on:
      - knowledge_base
    networks:
      - knowledge-base-web-network

  knowledge_base:
    build: .
    image: knowledge_base:1.1.2
    restart: always
    command: /bin/bash /app/run.sh
    env_file:
      - /etc/sysconfig/taged

    deploy:
      resources:
        limits:
          memory: "300M"
        reservations:
          memory: "200M"

    volumes:
      # Чтобы добавлять новые изображения и книги
      - ./media:/app/media
      - ./static:/app/static
      # База данных
      - ./db.sqlite3:/app/db.sqlite3
    depends_on:
      - elasticsearch
      - redis
    networks:
      - knowledge-base-web-network
      - knowledge-base-elasticsearch-network

  redis:
    image: redis
    restart: always
    networks:
      - knowledge-base-elasticsearch-network
    deploy:
      resources:
        limits:
          memory: "100M"
        reservations:
          memory: "50M"

networks:
  # Создаем свою сеть, чтобы не было конфликтов с уже существующей
  knowledge-base-web-network:
    ipam:
      driver: default
      config:
        - subnet: '10.198.198.0/29'
  knowledge-base-elasticsearch-network:
    ipam:
      driver: default
      config:
        - subnet: '10.199.198.0/29'
